package com.app.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.GeneralSecurityException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.ShortBufferException;
/**
 * Created by Administrator on 2015/11/11.
 */

/**
 * 默认使用des对文件进行加解密，对于java的web端也能解密，android加密的文件；
 */
public class Utils_FileCipher {

    public static int SUCCESS = 0;
    public static int FAILED_ABOUT_FILE = -1;
    public static int FAILED_ABOUT_PSW = -2;


    /*
    private static String type = "DES";  // AES
    private static String mode = "/ECB/PKCS5Padding";
    private static Utils_FileCipher mCipherUtil;

    public static Utils_FileCipher getInstance() {
        if (mCipherUtil == null) {
            synchronized (Utils_FileCipher.class) {
                if (mCipherUtil == null) {
                    mCipherUtil = new Utils_FileCipher();
                }
            }
        }
        return mCipherUtil;
    }


    public byte[] encrypt_test(Context context, String password, String content) throws GeneralSecurityException {
        Key key = getKey(password);
        Cipher cipher = Cipher.getInstance(type + mode);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] result = cipher.doFinal(content.getBytes());
        return result;

    }

    public String decrypt_test(Context context, String password, byte[] content) throws GeneralSecurityException {
        Key key = getKey(password);
        Cipher cipher = Cipher.getInstance(type + mode);
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] result = cipher.doFinal(content);
        return new String(result);
    }

    private static Key getKey(String secret) throws GeneralSecurityException {

        KeyGenerator kgen = KeyGenerator.getInstance(type);
        SecureRandom sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");
        sr.setSeed(secret.getBytes());
        kgen.init(128, sr);
        SecretKey secretKey = kgen.generateKey();
        return secretKey;
    }
    */


    /**
     * 把文件srcFile加密后存储为destFile
     *
     * @param srcFile 加密前的文件
     * @throws GeneralSecurityException
     * @throws IOException
     */
    public static int encrypt(String password, String srcFile, String destFile) throws GeneralSecurityException, IOException {

        Cipher cipher = DES.encrypt_init(password.getBytes());

        FileInputStream fis = null;
        FileOutputStream fos = null;
        int result = FAILED_ABOUT_FILE;
        try {

            fis = new FileInputStream(srcFile);
            fos = new FileOutputStream(Utils.mkdirFiles(destFile));

            result = crypt(fis, fos, cipher);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return FAILED_ABOUT_FILE;
        } catch (IOException e) {
            e.printStackTrace();
            return FAILED_ABOUT_FILE;
        } finally {
            if (fis != null) {
                fis.close();
            }
            if (fos != null) {
                fos.close();
            }


            return result;
        }

    }



    public static int decrypt(String password, String srcFile, String destFile) throws GeneralSecurityException, IOException {

        Cipher cipher = DES.decrypt_init(password.getBytes());

        FileInputStream fis = null;
        FileOutputStream fos = null;
        int result = FAILED_ABOUT_FILE;
        try {
            fis = new FileInputStream(srcFile);
            fos = new FileOutputStream(Utils.mkdirFiles(destFile));

            result = crypt(fis, fos, cipher);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return FAILED_ABOUT_FILE;
        } catch (IOException e) {
            e.printStackTrace();
            return FAILED_ABOUT_FILE;
        } finally {
            if (fis != null) {
                fis.close();
            }
            if (fos != null) {
                fos.close();
            }


            return result;

        }


    }



    /**
     * 加密解密流
     *
     * @param in     加密解密前的流
     * @param out    加密解密后的流
     * @param cipher 加密解密
     * @throws IOException
     * @throws GeneralSecurityException
     */
    private static int crypt(InputStream in, OutputStream out, Cipher cipher) {
        int blockSize = cipher.getBlockSize() * 1000;
        int outputSize = cipher.getOutputSize(blockSize);

        byte[] inBytes = new byte[blockSize];
        byte[] outBytes = new byte[outputSize];

        int inLength = 0;
        boolean more = true;
        try {
            while (more) {
                inLength = in.read(inBytes);
                if (inLength == blockSize) {
                    int outLength = cipher.update(inBytes, 0, blockSize, outBytes);
                    out.write(outBytes, 0, outLength);
                } else {
                    more = false;
                }
            }
            if (inLength > 0)
                outBytes = cipher.doFinal(inBytes, 0, inLength);
            else
                outBytes = cipher.doFinal();
            out.write(outBytes);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return FAILED_ABOUT_FILE;
        } catch (IOException e) {
            e.printStackTrace();
            return FAILED_ABOUT_FILE;
        } catch (BadPaddingException e) {
            e.printStackTrace();
            return FAILED_ABOUT_PSW;
        } catch (ShortBufferException e) {
            e.printStackTrace();
            return FAILED_ABOUT_PSW;
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
            return FAILED_ABOUT_PSW;
        }

        return SUCCESS;

    }
}